package blackitheima1.Output;

import java.io.*;
import java.nio.charset.Charset;

//转换流
public class MyOutput1 {
    public static void main(String[] args) throws IOException {
        //method1();
       // method2();
        //jdk11以后 //创建字符输入流 读数据
        FileReader fr=new FileReader("C:\\Users\\BEI\\Desktop\\a.txt", Charset.forName("gbk"));
        int ch;
        while((ch=fr.read())!=-1){
            System.out.println((char)ch);
        }
        //释放资源
        fr.close();



    }

    private static void method2() throws IOException {
        //转换流  读取数据  定义字码表 防止乱码
        InputStreamReader is=new InputStreamReader(
                new FileInputStream("C:\\Users\\BEI\\Desktop\\a.txt"),"gbk");
        int ch;
        while((ch=is.read())!=-1){
            System.out.println((char)ch);
        }
        //释放资源
        is.close();

        //转换流 写数据
        OutputStreamWriter os=new OutputStreamWriter(
                new FileOutputStream("C:\\Users\\BEI\\Desktop\\b.txt"),"UTF-8");
        //添加数据
        os.write("俗不可耐");
        //释放资源
        os.close();
    }

    private static void method1() throws IOException {
        //采用普通的字符输入流 读取汉字 举例 会出现乱码
        FileReader fr=new FileReader("C:\\Users\\BEI\\Desktop\\a.txt");
        int ch;
        while((ch=fr.read())!=-1){
            System.out.println((char) ch);
        }
        fr.close();
    }
}
